package com.demo.codeGenerator.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;


public class CodeGenerator {
	
	private Configuration configuration;
	
	public CodeGenerator(){
		configuration = new Configuration();
		configuration.setClassForTemplateLoading(CodeGenerator.class, "/template/");		
	}
	
	public void genCode(){
		readConfig();
		genEntity();
	}
	
	public void readConfig() {
		StringBuffer filePathSb = new StringBuffer();
//		new Properties().load(new FileInputStream(name));
	}

	public void genEntity() {
		
	}

	public static void main(String[] args) {
		String packageName = "com.demo.service.system"; 
		String templateName = "Service.ftl"; 
		String[] fileName = {"RoleService","MenuService","OperationService","ResourceService","PermissionService","RoleAdminRelationService"}; 
		String[] entityName = {"Role","Menu","Operation","Resource","Permission","RoleAdminRelation"};
		String[] entityName_zh = {"角色","菜单","操作","资源","权限","管理员角色"};
		for (int i = 0; i < entityName.length; i++) {
			new CodeGenerator().generatorServiceFile(templateName, packageName, fileName[i], entityName[i], entityName_zh[i]);
		}
		packageName = "com.demo.service.system.impl"; 
		templateName = "ServiceImpl.ftl"; 
		String[] fileName_impl = {"RoleServiceImpl","MenuServiceImpl","OperationServiceImpl","ResourceServiceImpl","PermissionServiceImpl","RoleAdminRelationServiceImpl"}; 
		for (int i = 0; i < entityName.length; i++) {
			new CodeGenerator().generatorServiceFile(templateName, packageName, fileName_impl[i], entityName[i], entityName_zh[i]);
		}
	}
	
	public void generatorServiceFile(String templateName, String packageName, String fileName, String entityName, String entityName_zh) {
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("entityName_zh", entityName_zh);
		rootMap.put("entityName", entityName);
		rootMap.put("packageName", packageName);
		rootMap.put("fileName", fileName);
		String path = this.getClass().getClassLoader().getResource(".").getPath();	
		StringBuffer filePath  = new StringBuffer(path);
		if (packageName != null && packageName.length()>0) {
			filePath.append(packageName.replace(".", File.separator));
		}
		if (filePath.charAt(filePath.length()-1)!=File.separatorChar) {
			filePath.append(File.separatorChar);
		}
		filePath.append(fileName);
		if (!filePath.toString().toLowerCase().endsWith(".java")) {
			filePath.append(".java");
		}
		Writer out = null;
		try {
			Template template = configuration.getTemplate(templateName);
			out = new FileWriter(filePath.toString());
			template.process(rootMap, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} finally{
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
